<template>
  <div class="login">
    <img class="bj" src="@/assets/login/bj.jpg" alt="" />
    <div class="bjbox"></div>
    <div class="login-box">
      <div class="login-form">
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules">
          <div class="login-form-title">
            <img
              src="@/assets/login/icon_logo.png"
              style="width: 260px; height: auto"
              alt=""
            />

            <!-- <span class="title-label">满曦堂餐厅</span> -->
          </div>
          <div class="login_text">
            商家后台登录
          </div>
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              type="text"
              auto-complete="off"
              placeholder="账号"
              prefix-icon="iconfont icon-user"
            />
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              type="password"
              placeholder="密码"
              prefix-icon="iconfont icon-lock"
              @keyup.enter.native="handleLogin"
            />
          </el-form-item>
          <el-form-item style="width: 100%">
            <el-button
              :loading="loading"
              class="login-btn"
              size="medium"
              type="primary"
              style="width: 100%"
              @click.native.prevent="handleLogin"
            >
              <span v-if="!loading">登录</span>
              <span v-else>登录中...</span>
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import { Component, Vue, Watch } from 'vue-property-decorator'
import { Route } from 'vue-router'
import { Form as ElForm, Input } from 'element-ui'
import { UserModule } from '@/store/modules/user'
import { isValidUsername } from '@/utils/validate'

@Component({
  name: 'Login',
})
export default class extends Vue {
  private validateUsername = (rule: any, value: string, callback: Function) => {
    if (!value) {
      callback(new Error('请输入用户名'))
    } else {
      callback()
    }
  }
  private validatePassword = (rule: any, value: string, callback: Function) => {
    if (value.length < 6) {
      callback(new Error('密码必须在6位以上'))
    } else {
      callback()
    }
  }
  private loginForm = {
    username: '',
    password: '',
  } as {
    username: String
    password: String
  }

  loginRules = {
    username: [{ validator: this.validateUsername, trigger: 'blur' }],
    password: [{ validator: this.validatePassword, trigger: 'blur' }],
  }
  private loading = false
  private redirect?: string

  @Watch('$route', { immediate: true })
  private onRouteChange(route: Route) {}

  // 登录
  private handleLogin() {
    ;(this.$refs.loginForm as ElForm).validate(async (valid: boolean) => {
      if (valid) {
        this.loading = true
        await UserModule.Login(this.loginForm as any)
          .then((res: any) => {
            if (String(res.code) === '1') {
              this.$router.push('/')
            } else {
              // this.$message.error(res.msg)
              this.loading = false
            }
          })
          .catch(() => {
            // this.$message.error('用户名或密码错误！')
            this.loading = false
          })
      } else {
        return false
      }
    })
  }
}
</script>

<style lang="scss">
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  width: 100%;
  .bj {
    position: absolute;
    z-index: -1;
    width: 100%;
    height: 100%;
  }
  .bjbox {
    position: absolute;
    z-index: -1;
    width: 100%;
    height: 100%;
    background-color: #000000;
    opacity: 0.1;
  }
}

.login-box {
  width: 400px;
  height: 350px;
  // border-radius: 8px;
  display: flex;
  border-radius: 10px;
  overflow: hidden;
  box-shadow: 0px 0px 10px #ffffff;
}

.title {
  margin: 0px auto 10px auto;
  text-align: left;
  color: #707070;
}

.login-form {
  background: #ffffff;
  width: 100%;
  display: flex;
  justify-content: center;
  // align-items: center;
  padding: 40px 30px;
  .el-form {
    width: 100%;
    height: 307px;
  }
  .el-form-item {
    margin-bottom: 20px;
  }
  .el-form-item.is-error .el-input__inner {
    // border: 0 !important;
    border-bottom: 1px solid #fd7065 !important;
    background: #fff !important;
  }
  .input-icon {
    height: 32px;
    width: 18px;
    margin-left: -2px;
  }
  .el-input__inner {
    border: 1px solid #e9e9e8;
    border-radius: 3px;
    font-size: 14px;
    font-weight: 400;
    color: #333333;
    height: 34px;
    line-height: 34px;
  }
  .el-input__prefix {
    left: 8px;
  }
  .el-input--prefix .el-input__inner {
    padding-left: 30px;
  }
  .el-input__inner::placeholder {
    color: #aeb5c4;
  }
  .el-form-item--medium .el-form-item__content {
    line-height: 32px;
  }
  .el-input--medium .el-input__icon {
    line-height: 32px;
  }
}

.login_text {
  font-size: 20px;
  margin-bottom: 20px;
}

.login-btn {
  border-radius: 4px;
  padding: 11px 20px !important;
  margin-top: 10px;
  font-weight: 500;
  font-size: 16px;
  border: 0;
  font-weight: 500;
  color: #ffffff;
  // background: #09a57a;
  background-color: #42c2ff;
  &:hover,
  &:focus {
    // background: #09a57a;
    background-color: #42c2ff;
    color: #ffffff;
  }
}
.login-form-title {
  height: 36px;
  display: flex;
  justify-content: center;
  align-items: center;
  margin-bottom: 30px;
  .title-label {
    font-weight: 500;
    font-size: 20px;
    color: #333333;
    margin-left: 10px;
  }
}
</style>
